<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/10/10 0010
 * Time: 15:42
 */
namespace Admin\Controller;

use Think\Controller;
class CommonController extends Controller {
    var $link;
    var $site;
    var $about;
    var $category;
    var $photo;
    var $article;
    function __construct()
    {
        parent::__construct();
        $this->check_login();
        $this->link = M('Link');
        $this->site = M('Site');
        $this->about = M('About');
        $this->category = M('Category');
        $this->photo = M('Photo');
        $this->article = M('Article');
    }

    /***
     * 检查用户是否登录
     */
    public function check_login()
    {
        //如果有session，那么发送到模板
        if(isset($_SESSION['user'])){
            $this->assign('user',$_SESSION['user']);
            return;
        }
        //判断是否有COOKIE,如果没有，跳到登录页
        if(!isset($_COOKIE['token'])){
            $this->error('您还没有登录，请登录后再试',U('User/login'));
        }
        //如果token与数据库里面的不一样，表示用户伪造了cookie
        $token = $_COOKIE['token'];
        $User = M('User');
        $user = $User->where("token = '$token'")->find();
        if(!$user){
            $this->error('请不要非法登录');
        }
        $_SESSION['user'] = $user;
        $this->assign('user',$user);
    }
}